﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FacturIT.Service.Company;
using FacturIT.Service.PaymentType;
using FacturIT.Service.Project;
using FacturIT.Service.User;
using FacturIT.Service.CompanyProject;
using FacturIT.Service.WorkingHour;
using FacturIT.Service.UserCompany;



namespace FacturIT.Service
{
    public class FacturITManager
    {
        #region Singleton

        private static FacturITManager instance = null;
        public static FacturITManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new FacturITManager();
                return instance;
            }
        }

        #endregion

        #region Services
        private CompanyFactory companyFactory;
        private CompanyFactory CompanyFactory
        {
            get
            {
                if (companyFactory == null)
                    companyFactory = new CompanyFactory();

                return companyFactory;
            }
        }
        #endregion

        #region Company

        #region usercompany
        public UserCompanyDTO AddUserToCompany(int owner, int companyid)
        {
            return UserCompanyFactory.Instance.GetUserCompanyService().AddUserToCompany(owner, companyid);
        }

        public IEnumerable<UserCompanyDTO> ReadMyCompanies(int userid)
        {
            return UserCompanyFactory.Instance.GetUserCompanyService().ReadMyCompanies(userid);
        }
        #endregion

        public CompanyDTO AddCompany(string name, string street, string pc, string city, string country, string phone, int? owner)
        {
            return CompanyFactory.Instance.GetCompanyService().Addcompany(name, street, pc, city, country, phone, owner);
        }
        
        public CompanyDTO UpdateCompany(string name, string street, string pc, string city, string country, string phone)
        {
            return CompanyFactory.Instance.GetCompanyService().UpdateCompany(name, street, pc, city, country, phone);
        }

        public CompanyDTO ReadCompany(int companyID)
        {
            return CompanyFactory.Instance.GetCompanyService().ReadCompany(companyID);
        }

        public IEnumerable<CompanyDTO> ReadMyOwnedCompanies(int userID)
        {
            return CompanyFactory.Instance.GetCompanyService().ReadMyOwnedCompanies(userID);
        }

        public IEnumerable<CompanyDTO> ReadAllCompanies()
        {
            return CompanyFactory.Instance.GetCompanyService().ReadAllCompanies();
        }

        public CompanyDTO ReadCompanyByName(string companyname)
        {
            return CompanyFactory.Instance.GetCompanyService().ReadCompanyByName(companyname);
        }

        

        #endregion

        #region Project

        #region ProjectUsers
        public CompanyProjectDTO AddCompanyToProject(int projectid, int userid)
        {
            return CompanyProjectFactory.Instance.GetCompanyProjectService().AddCompanyToProject(projectid, userid);
        }
        public CompanyProjectDTO ReadCompanyProjectByProjectAndCompany(int projectid, int companyid)
        {
            return CompanyProjectFactory.Instance.GetCompanyProjectService().ReadCompanyProjectByProjectAndCompany(projectid, companyid);
        }

        public IEnumerable<CompanyProjectDTO> ReadAllCompaniesFromProject(int projectID)
        {
            return CompanyProjectFactory.Instance.GetCompanyProjectService().ReadAllCompaniesFromProject(projectID);
        }
        #endregion

        #region PaymentTypes
        public IEnumerable<PaymentTypeDTO> ReadAllPaymentTypes()
        {
            return PaymentTypeFactory.Instance.GetPaymentTypeService().ReadAllPaymentTypes();
        }

        public PaymentTypeDTO ReadPaymentType(int paymentTypeID)
        {
            return PaymentTypeFactory.Instance.GetPaymentTypeService().ReadPaymentType(paymentTypeID);
        }
        #endregion


        public ProjectDTO AddProject(string name, DateTime start, DateTime end, bool active, int paymenttype, int customer)
        {
            return ProjectFactory.Instance.GetProjectService().AddProject(name, start, end, active, paymenttype, customer);
        }

        public ProjectDTO ReadProject(int id)
        {
            return ProjectFactory.Instance.GetProjectService().ReadProject(id);
        }

        public IEnumerable<ProjectDTO> ReadAllProjectsFromCompany(int companyID)
        {
            return ProjectFactory.Instance.GetProjectService().ReadAllProjectsFromCompany(companyID);
        }

        public IEnumerable<ProjectDTO> ReadProjectByID(int projectid)
        {
            return ProjectFactory.Instance.GetProjectService().ReadProjectByID(projectid);
        }

        public ProjectDTO GetProjectByValues(string name, DateTime start, DateTime? end, bool p, int paymenttype, int customer, int company)
        {
            return ProjectFactory.Instance.GetProjectService().GetProjectByValues(name, start, end, p, paymenttype, customer, company);
        }

        public ProjectDTO ReadPaymentTypeFromProject(int projectID)
        {
            return ProjectFactory.Instance.GetProjectService().ReadPaymentTypeFromProject(projectID);
        }

        public IEnumerable<ProjectDTO> ReadAllActiveProjectsFromCompany(int companyid)
        {
            return ProjectFactory.Instance.GetProjectService().ReadAllActiveProjectsFromCompany(companyid);
        }
        #endregion

        #region  User
        public UserDTO AddUser(string lname, string fname, string uname, string address, string pc, string city, string country, string phone, string btw)
        {
            return UserFactory.Instance.GetUserService().AddUser(fname, lname, uname, address, pc, city, country, phone, btw);
        }

        public UserDTO ReadUser(int userID)
        {
            return UserFactory.Instance.GetUserService().ReadUser(userID);
        }

        public UserDTO ReadUserFromDatabase(string username, string password)
        {
            return UserFactory.Instance.GetUserService().ReadUserFromDatabase(username, password);
        }
        #endregion

        #region WorkingHour
        /*public WorkingHourDTO AddWorkingHour(int projectuserid, DateTime starttime, DateTime endtime)
        {
            return WorkingHourFactory.Instance.GetWorkingHourervice().AddWorkingHour(projectuserid,starttime, endtime, false);
        }*/
        public WorkingHourDTO AddWorkingHour(int projectuserid, DateTime starttime, DateTime endtime, bool charged)
        {
            WorkingHourDTO wh = WorkingHourFactory.Instance.GetWorkingHourervice().AddWorkingHour(projectuserid, starttime, endtime, charged);
            return wh;
        }

        public IEnumerable<WorkingHourDTO> ReadAllMyTotalWorkingHoursForProject(int companyProjectID)
        {
            return WorkingHourFactory.Instance.GetWorkingHourervice().ReadAllMyTotalWorkingHoursForProject(companyProjectID);
        }

        public IEnumerable<WorkingHourDTO> ReadTotalWorkingHoursForProject(int projectID)
        {
            return WorkingHourFactory.Instance.GetWorkingHourervice().ReadTotalWorkingHoursForProject(projectID);
        }

        public IEnumerable<WorkingHourDTO> ReadMyTotalChargedForProject(int companyProjectID)
        {
            return WorkingHourFactory.Instance.GetWorkingHourervice().ReadMyTotalChargedForProject(companyProjectID);
        }

        public IEnumerable<WorkingHourDTO> ReadAllWorkingHoursFromUserProjectInPeriodAndSetCharged(int companyprojectid, DateTime van, DateTime tot)
        {
            return WorkingHourFactory.Instance.GetWorkingHourervice().ReadAllWorkingHoursFromUserProjectInPeriodAndSetCharged(companyprojectid, van, tot);
        }

        public IEnumerable<WorkingHourDTO> ReadAllWorkingHoursFromUserProjectInPeriodCharged(int companyprojectid, DateTime van, DateTime tot)
        {
            return WorkingHourFactory.Instance.GetWorkingHourervice().ReadAllWorkingHoursFromUserProjectInPeriodCharged(companyprojectid, van, tot);
        }

        #endregion



        public bool IsLoggedID()
        {
            if (Convert.ToBoolean(System.Web.HttpContext.Current.Session["LoggedIn"]) == true)
                return true;
            else return false;
        }

        
    }
}